# -*- coding: utf-8 -*-
# #############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################


from osv import fields, osv


class init_product_category(osv.osv):
    _inherit = "product.category"
    _columns = {
        'group_id': fields.many2one('res.groups', 'Group '),

    }

    def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=100):
        if not args:
            args = []
        ids = ids = self.search(cr, user, [('name', '=', name)] + args, limit=limit)
        if not ids:
            ids = self.search(cr, user, [('name', operator, name)] + args, limit=limit)

        return self.name_get(cr, user, ids, context)

    def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
        if context is None:
            context = {}

        group_ids = [group.id for group in self.pool.get('res.users').browse(cr, uid, uid).groups_id]
        if not context.get('search_all', False):
            args.append(('group_id', 'in', group_ids + [False]))

        return super(init_product_category, self).search(cr, uid, args, offset, limit,
                                                         order, context=context, count=count)

    def write(self, cr, uid, ids, vals, context=None):
        if vals.get('group_id', False):
            categ_ids = self.search(cr, uid, [('parent_id', 'in', ids), ('group_id', '=', False)],
                                    context={'search_all': True})
            if categ_ids:
                self.write(cr, uid, categ_ids, {'group_id': vals.get('group_id', False)}, context)
        return super(init_product_category, self).write(cr, uid, ids, vals, context)

    def create(self, cr, uid, vals, context=None):
        id = super(init_product_category, self).create(cr, uid, vals, context)
        if vals.get('group_id', False):
            categ_ids = self.search(cr, uid, [('id', 'child_of', [id]), ('group_id', '=', False)],
                                    context={'search_all': True})
            self.write(cr, uid, categ_ids, {'group_id': vals.get('group_id', False)}, context)
        return id


init_product_category()


class init_product_product(osv.osv):
    _inherit = "product.product"
    _columns = {

    }

    def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
        if context is None:
            context = {}

        group_ids = [group.id for group in self.pool.get('res.users').browse(cr, uid, uid).groups_id]

        if not context.get('search_all', False):
            categ_ids = self.pool.get('product.category').search(cr, uid, [('group_id', 'in', group_ids + [False])])
            args.append(('categ_id', 'in', categ_ids))

        return super(init_product_product, self).search(cr, uid, args, offset, limit,
                                                        order, context=context, count=count)


init_product_product()


# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
